import time
import requests
import json
import pymysql
from project01.apps import app


@app.task
def get_sina_news():
    """

    :param url:
    """
    for i in range(1, 6):
        now = int(time.time() * 1000)
        url = 'https://feed.sina.com.cn/api/roll/get?pageid=121&lid=1356&num=20&versionNumber=1.2.4&page={' \
              'page}&encode=utf-8&callback=feedCardJsonpCallback&_={time}'.format(page=i, time=now)
        headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
        }
        res = requests.get(url, headers)
        text = res.content.decode(encoding='utf-8')
        conn = pymysql.connect(
            host='127.0.0.1',
            port=3306,
            user='',
            password='',
            database='sina_news',
            charset='utf8mb4')
        cursor = conn.cursor()
        try:
            text = text.split('try{feedCardJsonpCallback(')[-1]
            text = text.split(');}catch(e){};')[0]
            json_data = json.loads(text)
            result = json_data.get('result')
            if result:
                data = result.get('data')
                if isinstance(data, list):
                    sql = """insert into news(oid, title, stitle, intro, ctime, url) value(%s, %s, %s, %s, %s, %s)"""
                    selet_sql = """select title, url, ctime from news WHERE title=%s AND url=%s AND ctime=%s"""
                    for item in data:
                        oid = item.get('oid')
                        title = item.get('title')
                        stitle = item.get('stitle')
                        intro = item.get('intro')
                        ctime = item.get('ctime')
                        url = item.get('url')
                        exites = cursor.execute(selet_sql, (title, url, ctime))
                        # print('是否存在数据：', exites)
                        if exites:
                            continue
                        else:
                            cursor.execute(
                                sql, (oid, title, stitle, intro, ctime, url))
                            conn.commit()
                    conn.close()
        except Exception as error:
            print(error)
        finally:
            time.sleep(1)
    return 'sina news save success'
